<template>
  <t-link
    v-if="userStore.userInfo.id==1"
    theme="primary"
    @click="handleOpenDialog('update', { orderId: props.id })"
  >
    手动
  </t-link>
  <!-- 自定义对话框 -->
  <custom-dialog
    ref="customDialogRef"
    v-slot="{ formData }"
    :api-map="{ update: setSendNotice }"
    header="手动"
    @request-success="handleDialogRequestSuccess"
  >
    <t-form-item label="模板id" name="messageId" :rules="[{ required: true, message: '请选择模板id' }]">
      <t-select
        v-model:value="formData.messageId"
        :options="messageList"
        :keys="{ value: 'id', label: 'modelName' }"
        placeholder="请选择模板id"
        clearable
        filterable
        @clear="formData.messageId = null"
      />
    </t-form-item>
    
  </custom-dialog>
</template>

<script setup lang="ts">
import { ref, useTemplateRef } from 'vue';

import { setSendNotice } from '@/api/order/basic';
import CustomDialog from '@/components/custom/dialog/index.vue';
import { useDictionaryStore, useUserStore } from '@/store';
import { messageTemplateCRUD } from '@/api/config/message-template';
import { MessagePlugin } from 'tdesign-vue-next';
// 用户仓库
const userStore = useUserStore();
// 字典仓库
const dictionaryStore = useDictionaryStore();
// props
const props = defineProps({
  // 订单id
  id: {
    type: Number,
    required: true,
  },
});
// emits
const emits = defineEmits(['requestSuccess']);
// 自定义对话框ref
const customDialogRef = useTemplateRef('customDialogRef');
// 处理打开对话框
const handleOpenDialog = (mode: 'create' | 'read' | 'update', params: Record<string, any>) => {
  customDialogRef.value.open(mode, params);
  handleGetMessageList()
};
// 处理对话框请求成功
const handleDialogRequestSuccess = (result: Record<string, any>) => {
  emits('requestSuccess', result);

};
const messageList = ref([])
const handleGetMessageList = () => {
  const params = {
    pageSize: -1,
  };
  messageTemplateCRUD.page(params).then((data) => {
    messageList.value = data.list;
    })
    .catch(({ message }) => {
      MessagePlugin.error(message);
    });
}
</script>
